package com.china08.yxyapi.api.home.resource;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.service.yxyresource.YxyResourceService;
import com.china08.yxyapi.util.FloatUtil;

import io.swagger.annotations.ApiOperation;

/**
 * 资源首页。推荐
 * 
 * @author renruihu
 *
 */
@CrossOrigin
@RestController
public class RecommendResourceController extends YxyApiBaseController {

    @Autowired
    private YxyResourceService yrs;

    @ApiOperation(value = "获得首页右侧推荐资源")
    @ResponseBody
    @RequestMapping(value = "/api_yw/home/res/recommend/lst", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> find(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) throws IllegalAccessException, InvocationTargetException {
        if (reqModel == null) {
            reqModel = new ReqModel();
        }
        List<RespModel> list = new ArrayList<RecommendResourceController.RespModel>();
        List<ElasticYxyResResource> esElasticYxyResResources = new ArrayList<ElasticYxyResResource>();
        if (reqModel.resType == 1) {
            esElasticYxyResResources = yrs.homeIndexResourceRecommendArea(reqModel.areaId, reqModel.bookContent,
                    reqModel.bookId);
        } else if (reqModel.resType == 2) {
            String schoolId = reqModel.schoolId;
            esElasticYxyResResources = yrs.homeIndexResourceRecommendSchool(schoolId, reqModel.bookContent,
                    reqModel.bookId);
        } else if (reqModel.resType == 3) {
            esElasticYxyResResources = yrs.homeIndexResourceRecommendYun(reqModel.bookContent, reqModel.bookId);
        }
        esElasticYxyResResources.forEach(x -> list.add(new RespModel(x.getTitle(), x.getPv(), x.getDownloadCount(),
                x.getId(), x.getIsDelete(), x.getConvertStatus(), x.getUploadTime(), x.getFormatId(),
                FloatUtil.dot1(x.getMark()), x.getExtension(), x.getIsVip(), x.getIsHighQualityYx(), x.getTypeId())));
        return list;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 1区域资源 2本校资源 3云校资源
        public Integer resType = 1;

        public String bookContent;

        // 区域id
        public String areaId;

        // 本校资源Id
        public String schoolId;

        //
        public String bookId;

        @Override
        public void autoCheck() {
            // Assert.notNull(StringUtils.defaultIfBlank(bookContent, null),
            // "章节不能为空");
        }

    }

    public static class RespModel extends YxyApiRespModel {

        // 标题
        public String title;

        // 浏览次数
        public Long pv = 0l;

        // 下载量
        public Long downloadCount = 0l;

        // 资源ID
        public String id;

        public Integer isDelete = 0;

        // 文件的状态信息 0初始上传， 1 转码中 ，2 转码成功 ，3 转码失败
        public Integer convertStatus = 0;

        // 上传时间
        public Date uploadTime = new Date();

        // 格式
        public String formatId;

        // 评分
        public float mark = 0f;

        // 文件扩展名
        public String extension;

        public Boolean isVip = false;

        public Integer isHighQualityYx = 0;
        // 类型
        public String typeId;

        public RespModel(String title, Long pv, Long downloadCount, String id, Integer isDelete, Integer convertStatus,
                Date uploadTime, String formatId, float mark, String extension, Boolean isVip, Integer isHighQualityYx,
                String typeId) {
            super();
            this.title = title;
            this.pv = pv;
            this.downloadCount = downloadCount;
            this.id = id;
            this.isDelete = isDelete;
            this.convertStatus = convertStatus;
            this.uploadTime = uploadTime;
            this.formatId = formatId;
            this.mark = mark;
            this.extension = extension;
            this.isVip = isVip;
            this.isHighQualityYx = isHighQualityYx;
            this.typeId = typeId;
        }

    }

}
